//
//  NavViewController.m
//  MVVM+RAC
//
//  Created by bopeng on 16/5/30.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "NavViewController.h"
#import "NavCell.h"
#import "UINavigationBar+Awesome.h"
#import "BarToolView.h"
#import "Header.h"
#import "UIScrollView+EmptyDataSet.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <HMSegmentedControl.h>

#define KScreenWidth  [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

#define NAVBAR_CHANGE_POINT (-186)

#define ZoomImvHeight 200.0f
#define BarViewH   50.0f

@interface NavViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIImageView *zoomImgView;
//@property (nonatomic, strong) BarToolView *toolView;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) HMSegmentedControl *segmentCtrl;


@end

@implementation NavViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    [self scrollViewDidScroll:self.tableView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    ScottWhiteStatus;
}

- (UIImageView *)zoomImgView {
    if (!_zoomImgView) {
        _zoomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -ZoomImvHeight-BarViewH, KScreenWidth, ZoomImvHeight)];
        _zoomImgView.image = [UIImage imageNamed:@"person_background"];
        _zoomImgView.contentMode = UIViewContentModeScaleAspectFill;
        _zoomImgView.clipsToBounds = YES;
        _zoomImgView.autoresizesSubviews = YES;
    }
    return _zoomImgView;
}

- (HMSegmentedControl *)segmentCtrl {
    if (!_segmentCtrl) {
        _segmentCtrl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"主页xxx",@"收藏",@"主页x",@"收藏",@"主页",@"收藏",@"主页xxxxx",@"收藏",@"主页",@"收藏"]];
        CGRect frame = CGRectMake(0, CGRectGetMaxY(self.zoomImgView.frame), KScreenWidth, BarViewH);
        _segmentCtrl.frame = frame;
        _segmentCtrl.backgroundColor = [UIColor greenColor];
        _segmentCtrl.selectionIndicatorHeight = 2.0f;
        _segmentCtrl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        _segmentCtrl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
        _segmentCtrl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor], NSFontAttributeName : [UIFont systemFontOfSize:20]};
        _segmentCtrl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor redColor]};
        _segmentCtrl.selectionIndicatorColor = [UIColor redColor];
        _segmentCtrl.selectedSegmentIndex = 0;
        _segmentCtrl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
        _segmentCtrl.borderType = HMSegmentedControlBorderTypeBottom;
        _segmentCtrl.borderColor = [UIColor lightGrayColor];
        _segmentCtrl.verticalDividerEnabled = YES;
        _segmentCtrl.indexChangeBlock = ^(NSInteger index){
            NSLog(@"%ld",index);
        };
    }
    return _segmentCtrl;
}

- (void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
}

- (void)setupTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView addSubview:self.zoomImgView];
    [self.tableView addSubview:self.segmentCtrl];
    
    self.tableView.contentOffset = CGPointMake(0, -ZoomImvHeight-BarViewH);
    self.tableView.contentInset = UIEdgeInsetsMake(ZoomImvHeight+BarViewH, 0, 0, 0);
}

#pragma mark - DZNEmptyDataSetSource
//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
//    return [UIImage imageNamed:@"home_nav_bar_fullscreen_back_icon_highlight"];
//}

//- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//    UILabel *label = [[UILabel alloc] init];
//    label.text = @"暂无数据";
//    label.textAlignment = NSTextAlignmentCenter;
//    return label;
//}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"暂无数据"];
    return str;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"点我刷新"];
    return str;
}


#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    NSLog(@"刷新数据");
    self.count = 25;
    [self.tableView reloadData];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NavCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NavCell class])];
    cell.titleLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.title = @"测试";
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%s",__FUNCTION__);
    UIColor *color = [UIColor cyanColor];
    
    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%.2f",offsetY);
    if (offsetY < - ZoomImvHeight - BarViewH) {
        CGRect frame = self.zoomImgView.frame;
        frame.origin.y = offsetY;
        frame.size.height = -offsetY - BarViewH;
        self.zoomImgView.frame = frame;
    }
    
    if (offsetY > -116.0) {
        self.segmentCtrl.frame = CGRectMake(0, scrollView.contentOffset.y + 64, KScreenWidth, BarViewH);
    }else{
        self.segmentCtrl.frame = CGRectMake(0, CGRectGetMaxY(self.zoomImgView.frame), KScreenWidth, BarViewH);
    }
    
    if (offsetY > NAVBAR_CHANGE_POINT) {
        CGFloat scale = ABS((offsetY - NAVBAR_CHANGE_POINT)) / 64.0;
        CGFloat alpha = MIN(1, scale);
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
        ScottBlackStatus
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
        ScottWhiteStatus
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
